#version 330 core
uniform vec3 Color;

struct Material {
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    vec3 emission;

    float shininess;
};
struct ParallelLight {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
struct BHXJVertex
{
    vec3 vertex;
    vec3 normal;
    vec2 texcoord;
};
in BHXJVertex vs_out;

uniform vec3 viewPos;
uniform Material material;
///平行光
uniform ParallelLight parallellight;

out vec4 FragColor;
void main()
{
    vec3 ambient  = parallellight.ambient  * material.ambient;

    vec3 norm = normalize(vs_out.normal);
    vec3 lightDir = normalize(parallellight.direction);

    float diff = max(dot(norm, -lightDir), 0.0);
    vec3 diffuse = parallellight.diffuse * diff*material.diffuse;



    //摄像机方向向量
    vec3 viewDir = normalize(viewPos - vs_out.vertex);
    //反射光向量
    vec3 reflectDir = reflect(lightDir, norm);
    //视角与法线夹角大于90度 或者 光线与法线夹角大于90  都不反光
    float sign1 =clamp(sign(dot(viewDir, norm)),0,1);
    float sign2 =clamp(sign(dot(-lightDir, norm)),0,1);

    float spec = pow(max(dot(viewDir, reflectDir)*sign1*sign2, 0.0), material.shininess);
    vec3 specular=parallellight.specular * spec * material.specular;

    vec3 result = ambient + diffuse + specular + material.emission;

    FragColor=vec4(result,1);
}
